#include "Checker.h"
#include "DataDefine.h"
#include "globalVariables.h"
#include "HitAnimation.h"

Card* getCard(int id);

Checker::Checker(UnitCard* card, int x, int y, bool isEnemy)
{
	this->card = (UnitCard*)getCard(card->id);
	xPos = x;
	yPos = y;
	this->isEnemy = isEnemy;
	hasMoved = false;
	isFrozen = false;
	health = card->maxHealth;
}

Checker::~Checker()
{
	delete[] card;
}

int Checker::paint(SDL_Renderer* renderer)
{
	//paint picture
	SDL_Rect rect = { (WINDOW_WIDTH - MAP_WIDTH) / 2 + xPos * BLOCK_WIDTH + MAP_SIDE, 
		(WINDOW_HEIGHT - MAP_HEIGHT) / 2 - MAP_CORRECTION + yPos * BLOCK_HEIGHT + MAP_SIDE, 
		BLOCK_WIDTH - 2 * MAP_SIDE, BLOCK_HEIGHT - 2 * MAP_SIDE };
	if (hasMoved)
	{
		SDL_RenderCopy(renderer, card->checkerGrayTexture, NULL, &rect);
	}
	else
	{
		SDL_RenderCopy(renderer, card->checkerTexture, NULL, &rect);
	}
	//side paint
	SDL_Rect sideRect = { (WINDOW_WIDTH - MAP_WIDTH) / 2 + xPos * BLOCK_WIDTH,
		(WINDOW_HEIGHT - MAP_HEIGHT) / 2 - MAP_CORRECTION + yPos * BLOCK_HEIGHT,
		BLOCK_WIDTH, BLOCK_HEIGHT };
	if (isEnemy)
	{
		SDL_RenderCopy(renderer, checkerSideEnemyTexture, NULL, &sideRect);
	}
	else
	{
		SDL_RenderCopy(renderer, checkerSideSelfTexture, NULL, &sideRect);
	}

	return 0;
}

int Checker::heal(int x)
{
	health += x;
	if (health > card->maxHealth)
	{
		health = card->maxHealth;
	}
	return 0;
}

int Checker::damage(int x)
{
	if (x <= card->armor)
	{
		return 0;
	}
	health -= (x - card->armor);
	//animation
	animationList.push_back(new HitAnimation(MAP_X+xPos*BLOCK_WIDTH, MAP_Y+yPos*BLOCK_HEIGHT));
	//death judge
	if (health <= 0)
	{
		//into graveyard
		if (isEnemy)
		{
			enemyPlayer->graveyard.push_back(getCard(card->id));
		}
		else
		{
			printf("battle destroy into graveyard\n");
			player->graveyard.push_back(getCard(card->id));
		}
		//destroy
		destroyX.push_back(xPos);
		destroyY.push_back(yPos);
	}
	return 0;
}